package com.dungnt.vtime.event;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class EventUtils {
	private Context context;
	private static EventUtils eventUtils;

	private EventUtils(Context context) {
		this.context = context;
	}

	public static EventUtils getEventUtilsInstance(Context mContext) {
		if (null == eventUtils) {
			eventUtils = new EventUtils(mContext);
		}
		return eventUtils;
	}

	public Uri insertEventToDB(EventItem eventItem) {
		ContentValues value = new ContentValues();
		value.put(EventProvider.TITLE, eventItem.getTitle());
		value.put(EventProvider.CONTENT, eventItem.getContent());
		value.put(EventProvider.FOLDER, eventItem.getFolder());
		value.put(EventProvider.TIME, eventItem.getTime());
		value.put(EventProvider.IMAGE, eventItem.getImg());
		return context.getContentResolver().insert(EventProvider.CONTENT_URI,
				value);
	}

	public ArrayList<EventItem> getEventsFromDB() {
		ArrayList<EventItem> eventList = new ArrayList<EventItem>();
		Cursor c = context.getContentResolver().query(
				EventProvider.CONTENT_URI, null, null, null, null);
		if (c != null) {
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				EventItem e = new EventItem();
				e.setTitle(c.getString(c.getColumnIndex(EventProvider.TITLE)));
				e.setContent(c.getString(c
						.getColumnIndex(EventProvider.CONTENT)));
				e.setTime(c.getLong(c.getColumnIndex(EventProvider.TIME)));
				e.setFolder(c.getInt(c.getColumnIndex(EventProvider.FOLDER)));
				e.setImg(c.getBlob(c.getColumnIndex(EventProvider.IMAGE)));
				eventList.add(e);
			}
			c.close();
		}
		return eventList;
	}
}
